<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>

<div role="region" id="region">
  <span role="none" id="span1">This is a<a id="link" href="#1">test</a></span>
  <span role="none" id="span2">of selection.</span>
</div>

<script>
  function verifySelection(anchorNode, anchorOffset, focusNode, focusOffset, selectionString) {
    const selection = getSelection();
    assert_equals(selection.anchorNode, anchorNode, 'anchorNode');
    assert_equals(selection.anchorOffset, anchorOffset, 'anchorOffset');
    assert_equals(selection.focusNode, focusNode, 'focusNode');
    assert_equals(selection.focusOffset, focusOffset, 'focusOffset');
    assert_equals(selection.toString(), selectionString, 'getSelection.toString()');
  }

  setup(() => {
    window.axRegion = accessibilityController.accessibleElementById('region');
    window.span1 = document.querySelector('span');
    window.text1 = span1.firstChild;
    window.span2 = document.querySelectorAll('span')[1];
    window.text2 = span2.firstChild;
    window.link = document.querySelector('a');
    window.linkText = link.firstChild;
    window.lineBreak = span1.nextSibling;
  });

  test(() => {
    axRegion.setSelection(axRegion, 0, axRegion, 0);
    verifySelection(text1, 0, text1, 0, '');
  }, 'Test creating a collapsed selection before the first character of the first span.');

  test(() => {
    axRegion.setSelection(axRegion, 0, axRegion, 1);
    verifySelection(text1, 0, span1, 1, 'This is a');
  }, 'Test creating a selection around the text in the first span.');

  test(() => {
    axRegion.setSelection(axRegion, 1, axRegion, 1);
    verifySelection(span1, 1, span1, 1, '');
  }, 'Test creating a collapsed selection before the link.');

  test(() => {
    axRegion.setSelection(axRegion, 1, axRegion, 2);
    verifySelection(span1, 1, lineBreak, 0, 'test');
  }, 'Test creating a selection around the link.');

  test(() => {
    axRegion.setSelection(axRegion, 3, axRegion, 3);
    verifySelection(text2, 0, text2, 0, '');
  }, 'Test creating a collapsed selection before the second span.');

  test(() => {
    axRegion.setSelection(axRegion, 3, axRegion, 4);
    verifySelection(text2, 0, span2, 1, 'of selection.');
  }, 'Test creating a selection around the second span.');

  test(() => {
    axRegion.setSelection(axRegion, 0, axRegion, 3);
    verifySelection(text1, 0, text2, 0, 'This is atest ');
  }, 'Test creating a selection from the first span and the link up to the second span.');

  test(() => {
    axRegion.setSelection(axRegion, 1, axRegion, 4);
    verifySelection(span1, 1, span2, 1, 'test of selection.');
  }, 'Test creating a selection around the link and the second span.');

  test(() => {
    axRegion.setSelection(axRegion, 0, axRegion, 4);
    verifySelection(text1, 0, span2, 1, 'This is atest of selection.');
  }, 'Test creating a selection around both spans.');

  test(() => {
    axRegion.setSelection(axRegion, 4, axRegion, 4);
    verifySelection(span2, 1, span2, 1, '');
  }, 'Test creating a collapsed selection after the second span.');
</script>
